package com.heima.schedule.feign;

import com.heima.apis.schedule.IScheduleClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.schedule.dtos.Task;
import com.heima.schedule.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: ScheduleClient
 * Description:
 * date: 2023/8/16 0016 15:08
 *
 * @author zHan
 */
@RestController
@Slf4j
@Api(tags = "延迟任务相关接口")
public class ScheduleClient implements IScheduleClient {

    @Autowired
    private TaskService taskService;

    /**
     * 添加任务
     *
     * @param task 任务对象
     * @return 任务id
     */
    @Override
    @PostMapping("/api/v1/task/add")
    @ApiOperation("添加任务")
    public ResponseResult addTask(@RequestBody Task task) {
        return ResponseResult.okResult(taskService.addTask(task));
    }

    /**
     * 取消任务
     *
     * @param taskId
     * @return
     */
    @Override
    @GetMapping("/api/v1/task/cancel/{taskId}")
    @ApiOperation("取消任务")
    public ResponseResult cancelTask(@PathVariable Long taskId) {
        return ResponseResult.okResult(taskService.cancelTask(taskId));
    }

    /**
     * 拉取任务
     *
     * @param type
     * @param priority
     * @return
     */
    @Override
    @GetMapping("/api/v1/task/poll/{type}/{priority}")
    @ApiOperation("拉取任务")
    public ResponseResult pullTask(@PathVariable("type") Integer type,
                                   @PathVariable("priority") Integer priority) {
        return ResponseResult.okResult(taskService.pullTask(type, priority));
    }
}
